#ifndef FTPSERVERPI_H
#define FTPSERVERPI_H

#include <QObject>
#include <QTcpSocket>
#include "serverconfig.h"

class FtpCommand
{
public:
    FtpCommand() = default;
    FtpCommand(const QByteArray& byteArray);

    QByteArray cmd() const;
    QByteArray arg() const;

    bool isNull() const;
    void setNull();

private:
    QByteArray m_cmd;
    QByteArray m_arg;
};

// Protocol Interpreter
class FtpServerPI : public QObject
{
    Q_OBJECT
public:
    explicit FtpServerPI(qintptr socketDescriptor, const ServerConfig& serverConfig,
                         QObject *parent = nullptr);
    ~FtpServerPI();

private slots:
    void init();
    void clean();
    void onReadyRead();
    void onError(QAbstractSocket::SocketError error);
    void onDisconnected();
    void startNextCommand();

private:
    void handleUSER();
    void handlePASS();
    void handlePORT();
    void handlePASV();
    void handleTYPE();
    void handleSTRU();
    void handleMODE();
    void handleCWD();
    void handleCDUP();
    void handleQUIT();
    void handleDELE();
    void handleRMD();
    void handleMKD();
    void handlePWD();
    void handleRNFR();
    void handleRNTO();
    void handleLIST();
    void handleNLST();
    void handleRETR();
    void handleSTOR();
    void handleSTOU();
    void handleAPPE();
    void handleALLO();
    void handleREST();
    void handleABOR();
    void handleSYST();
    void handleSTAT();
    void handleHELP();

private:
    qintptr      m_socketDescriptor;
    ServerConfig m_serverConfig;
    QTcpSocket  *m_socket = nullptr;
    QList<FtpCommand> m_commandQueue;
    FtpCommand   m_currentCommand;
    FtpCommand   m_lastCommand;
    bool         m_isAnonymous = false;
};


#endif // FTPSERVERPI_H
